package com.ymatou.iapi.m.couponservice.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.m.couponservice.parameter.CreateBean;
import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.service.CreateCall;
import com.ymatou.iapi.m.couponservice.service.CreateCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCouponConversionBatchRelation;
import com.ymttest.database.model.YmtCouponConversionBill;
import com.ymttest.database.model.YmtCouponConversionCode;
import com.ymttest.database.sqlwapper.YmtCouponConversionBatchRelationWapper;
import com.ymttest.database.sqlwapper.YmtCouponConversionBillWapper;
import com.ymttest.database.sqlwapper.YmtCouponConversionCodeWapper;

import jdk.nashorn.internal.ir.annotations.Ignore;

/**
 * 创建兑换码单据 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Create {
	private static CreateBean createBean;
	private static CreateCall createCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建兑换码单据");
	}

	@Before
	public void caseUp() {
		createBean = new CreateBean();
		createCall = new CreateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_001() {
		Logger.start(true, "创建兑换码随机单据-单batchcode-uat没配，过不了");
		try {

			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			createBean = mCouponserviceCallService.setDefaultRandomCreateBean(batchCodes);

			createCall.setData(createBean);
			createCall.callService();

			verifyReturn();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_002() {
		Logger.start(true, "创建兑换码自定义单据-单batchcode");
		try {

			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);

			createCall.setData(createBean);
			createCall.callService();

			verifyReturn();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_003() {
		Logger.start(true, "创建兑换码单据-batchcode不存在-调用服务的人验证,这边不验证");
		try {

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add("sdsdsd");

			createBean = mCouponserviceCallService.setDefaultRandomCreateBean(batchCodes);

			createCall.setData(createBean);
			createCall.callService();

			Logger.verifyEquals("3", createCall.getCode(), "验证返回code");
			Logger.verifyIsContains("未知错误，系统异常", createCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_004() {
		Logger.start(true, "创建兑换码随机单据-多条batchcode-uat没配，过不了");
		try {

			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall1 = couponCallService.createPlatformCoupon(createCouponBean);
			CreateCouponCall createCouponCall2 = couponCallService.createPlatformCoupon(createCouponBean);
			CreateCouponCall createCouponCall3 = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall1.getBatchCode());
			batchCodes.add(createCouponCall2.getBatchCode());
			batchCodes.add(createCouponCall3.getBatchCode());

			createBean = mCouponserviceCallService.setDefaultRandomCreateBean(batchCodes);

			createCall.setData(createBean);
			createCall.callService();

			verifyReturn();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_005() {
		Logger.start(true, "创建兑换码自定义单据-多条batchcode");
		try {

			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall1 = couponCallService.createPlatformCoupon(createCouponBean);
			CreateCouponCall createCouponCall2 = couponCallService.createPlatformCoupon(createCouponBean);
			CreateCouponCall createCouponCall3 = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();

			batchCodes.add(createCouponCall1.getBatchCode());
			batchCodes.add(createCouponCall2.getBatchCode());
			batchCodes.add(createCouponCall3.getBatchCode());

			createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);

			createCall.setData(createBean);
			createCall.callService();

			verifyReturn();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_Create_006() {
		Logger.start(true, "创建兑换码随机单据-单batchcode-压测生成100W张兑换码");
		try {

			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			createBean = mCouponserviceCallService.setDefaultRandomCreateBean(batchCodes);
			createBean.setGenerateQuantity(1000000);
			createCall.setData(createBean);
			createCall.callService();

			Date d1 = new Date();

			Logger.comment("开始时间："+d1.toLocaleString());
			
			YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
					.selectByBillName(createBean.getBillName()).get(0);
			
			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
			
			int flag = 0;
			while (ymtCouponConversionCodes.size()!= 100000 && flag < 50) {
				Thread.sleep(60000);
				ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
				
				flag++;
				
				Logger.comment("第"+flag+"分钟共生成"+ymtCouponConversionCodes.size()+"条记录，当前时间："+new Date().toLocaleString());				
			}
	
			Date d2=new Date();
			Logger.comment("总共花费时间:"+(d2.getTime()-d1.getTime())/(1000*60)+"分钟。");


		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_007() {
		Logger.start(true, "创建兑换码随机单据-创建指定兑换码的时候验证兑换码是否已经存在");
		try {

			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);

			createCall.setData(createBean);
			createCall.callService();
			
			createCall.setData(createBean);
			createCall.callService();

			Logger.verifyEquals("2", createCall.getCode(), "验证返回code");
			Logger.verifyIsContains("指定的兑换码已经存在", createCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	private void verifyReturn() throws InterruptedException {
		Logger.verifyEquals("0", createCall.getCode(), "验证返回code");
		Logger.verifyEquals("操作成功！", createCall.getMessage(), "验证返回Message");

		// 验证Ymt_CouponConversionBill表
		List<YmtCouponConversionBill> bills = new YmtCouponConversionBillWapper()
				.selectByBillName(createBean.getBillName());

		Logger.verifyEquals(1, bills.size(), "验证数据Ymt_CouponConversionBill中生成一条相应记录");

		if (bills.size() == 1) {
			YmtCouponConversionBill bill = bills.get(0);
			Logger.verifyEquals(createBean.getBillDes(), bill.getBilldes(), "验证数据Ymt_CouponConversionBill中字段Billdes");
			Logger.verifyEquals(createBean.getGenerateMethod(), bill.getGeneratemethod().intValue(),
					"验证数据Ymt_CouponConversionBill中字段GenerateMethod");
			Logger.verifyEquals(createBean.getCreateUser(), bill.getCreateuser(),
					"验证数据Ymt_CouponConversionBill中字段CreateUser");
			Logger.verifyEquals(createBean.getGenerateQuantity(), bill.getGeneratequantity(),
					"验证数据Ymt_CouponConversionBill中字段GenerateQuantity");
			Logger.verifyEquals(false, bill.getIsinvalid(), "验证数据Ymt_CouponConversionBill中字段IsInvalid");

			// 验证Ymt_CouponConversionBatchRelation表
			List<YmtCouponConversionBatchRelation> relations = new YmtCouponConversionBatchRelationWapper()
					.selectByConversionBillId(bill.getConversionbillid());

			Logger.verifyEquals(createBean.getCouponBatchCodes().size(), relations.size(), "验证数据Ymt_CouponConversionBatchRelation中生成一条相应记录");

			List<String> batchCodes = new ArrayList<>();
			
			for (YmtCouponConversionBatchRelation relation : relations) {
				batchCodes.add(relation.getCouponbatchcode());
			}
			
			Logger.verifyEquals(true, isListEqual(batchCodes,createBean.getCouponBatchCodes()), "验证数据Ymt_CouponConversionBatchRelation中生成一条相应记录");
			
			// 验证Ymt_CouponConversionCode表
			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
			
//			Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionBill中生成一条相应记录");

			// 自定义
			if(createBean.getGenerateMethod()==1)
			{
				Logger.verifyEquals(1, ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成一条相应记录");
				Logger.verifyEquals(createBean.getCustomConversionCode(), ymtCouponConversionCodes.get(0).getConversioncode(), "验证数据Ymt_CouponConversionCode中ConversionCode");

			}
			//随机生成
			else {
				
				int flag=0;
				
				while(ymtCouponConversionCodes.size()<createBean.getGenerateQuantity() && flag<100)
				{
					flag++;
					Thread.sleep(200);
					ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
				}
				
				Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");
				
				for (YmtCouponConversionCode ymtCouponConversionCode : ymtCouponConversionCodes) {
					Logger.verifyNotNull(ymtCouponConversionCode.getConversioncode(), "验证数据Ymt_CouponConversionCode中Conversioncode");
				}
				

			}
		}

	}
	
	private boolean isListEqual(List<String> list1, List<String> list2)
	{
		for (String str : list2) {
			if(!list1.contains(str))
				return false;
		}
		for (String str : list1) {
			if(!list2.contains(str))
				return false;
		}
		return true;
	}
	
}